package face.pay.cpn.mchinfo.service.impl;

import com.alibaba.fastjson.JSONArray;
import face.pay.cpn.AbstractApiService;
import face.pay.cpn.mchinfo.service.INewMchListService;
import face.pay.cpn.utils.request.RequestParam;
import face.pay.cpn.utils.response.ErrorCode;
import face.pay.cpn.utils.response.ResponseMessage;
import face.pay.dao.CpnMarketInfoDao;
import face.pay.dao.CpnOpenMchDao;
import face.pay.entity.CpnMarketInfo;
import face.pay.entity.CpnOpenMch;
import face.pay.util.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;


@Service
@Slf4j
public class NewMchListServiceimpl extends AbstractApiService implements INewMchListService {


    @Autowired
    private CpnMarketInfoDao cpnMarketInfoDao;


    @Autowired
    private CpnOpenMchDao cpnOpenMchDao;

    /**
     * 查询新增商家id
     *
     * @param jsonData
     * @return
     */
    @Override
    public ResponseMessage queryNewMchList(String jsonData) {
        return doApi(jsonData);
    }

    @Override
    public ResponseMessage doService(CpnMarketInfo cpnMarketInfo, RequestParam requestParam) {


        //查询开放商家的id


        String satartTime = requestParam.getData().get("queryTime").toString();

        if (DateUtil.dateParse(satartTime, "yyyy-MM-dd HH:mm:ss") == null) {
            return ResponseMessage.createFail(ErrorCode.PARAM_FAIL_CODE, "查询时间格式有误!");
        }
        log.info(">>>>>>>>>开始查找开放商家id market_id: {} ",cpnMarketInfo.getId());
        HashMap<String, Object> openMchRecord = new HashMap<>();
        openMchRecord.put("market_id", cpnMarketInfo.getId());
        openMchRecord.put("satartTime", satartTime);
        List<Integer> mchList = this.cpnOpenMchDao.queryCpnOpenMchList(openMchRecord).stream().map(CpnOpenMch::getMerchant_id).collect(Collectors.toList());
        log.info("<<<<<<<<<结束查找开放商家id market_id: {} 共查到:{} 条",cpnMarketInfo.getId(),mchList.size());
        HashMap<String, Object> dataMap = new HashMap<>();

        dataMap.put("mchList", mchList);


        return ResponseMessage.createSuccess(null, dataMap);

    }
}
